import {
	storeToRefs
} from 'pinia';
import initApi from '@/apis/init.js';
import {
	useUserStore
} from '@/store/user.js';
const {
	userInfo
} = storeToRefs(useUserStore());
export function getALPhoneNumber() {
	console.log('阿里巴巴');
	return new Promise((resolve, reject) => {
		my.getAuthCode({
			scopes: 'auth_user',
			success: res => {
				console.log(res, 'resfirst');
				const authCode = res.authCode;
				// 在服务端获取用户信息
				initApi.getApplyLogin({
					code: authCode
				}).then(resp => {
					console.log(resp, 'resp', res.code);
					if (resp.code == 20000) {

						uni.setStorageSync('token', resp.data.accessToken);
						userInfo.value.token = resp.data.accessToken
						console.log("获取 accessToken --->>");


						initApi.getUserInfo().then((resUserInfo) => {
							console.log('支付宝登陆数据', resUserInfo, resUserInfo.code == 20000);
							if (resUserInfo.code == 20000) {
								userInfo.value.mobile = resUserInfo.data.mobile;
								userInfo.value.username = resUserInfo.data.username;
								userInfo.value.info = resUserInfo.data;
								console.log(userInfo.value, '		console.log(userInfo.value);');

								uni.setStorageSync('info', JSON.stringify(resUserInfo.data));
								resolve('非异步方法的结果');
							} else {
								return uni.utils.toast(resUserInfo.message);
							}
						});

					} else {
						return uni.utils.toast(resp.message);
					}
				})
			},
			fail: err => {
				console.log('my.getAuthCode 调用失败', err)
			}
		});
	})

}

export function getWXphoneNumber() {
	console.log('微信小程序');
	wx.login({
		success(res) {
			console.log(res, 'res');
			const arr = {
				appId: 'wxd288833c4af82e86',
				code: res.code,
				encryptedData: null,
				iv: null
			};
			if (res.code) {
				initApi.getWXLogin(arr).then((res) => {
					if (res.code == 20000) {
						uni.setStorageSync('token', res.data.accessToken);
						userInfo.value.token = res.data.accessToken

						initApi.getUserInfo().then((res1) => {
							console.log('微信登陆数据', res1);
							if (res1.code == 20000) {
								userInfo.value.mobile = res1.data.mobile;
								userInfo.value.username = res1.data.username;
								userInfo.value.info = res1.data;
								uni.setStorageSync('info', JSON.stringify(res1.data));
							} else {
								return uni.utils.toast(res1.message);
							}
						});

					} else {
						return uni.utils.toast(res.message);
					}
				});
			} else {
				console.log('登录失败！' + res.errMsg);
			}
		}
	});
}
/* button 获取手机号码 apply 1 获取code 2 登陆 3获取电话信息 4 解密电话 */
export function getPhoneNumber() {

	// 登陆 
	my.getAuthCode({
		scopes: 'auth_user',
		success: res => {
			console.log(res, 'resfirst');
			const authCode = res.authCode;
			// 在服务端获取用户信息
			initApi.getApplyLogin({
				code: authCode
			}).then(resp => {
				console.log(resp, 'resp', res.code);
				if (resp.code == 20000) {

					uni.setStorageSync('token', resp.data.accessToken);
					userInfo.value.token = resp.data.accessToken
					console.log("获取 accessToken --->>");
					my.getPhoneNumber({
						success: (resPhone) => {
							console.log(resPhone, 'getPhoneNumber');
							const params = JSON.parse(resPhone.response)

							// 解密电话号码
							initApi.getApllyAuth({
								encryptedData: params.response,
								sign: params.sign
							}).then((resp1) => {
								console.log('成功 解密电话号码', resp1);
								if (resp1.code == 20000) {
									// 手机号
									if (resp1.data.accessToken) {
										uni.setStorageSync('token', resp1.data.accessToken);
										userInfo.value.token = resp1.data.accessToken
										userInfo.value.mobile = resp1.data.mobile
										console.log("accessToken 替换");
									}
									console.log('20000', resp1.data);
									userInfo.value.info = resp1.data;
									uni.setStorageSync('info', JSON.stringify(resp1.data));
								} else {
									return uni.utils.toast(resp1.message);
								}
							});
						},
						fail: (err) => {
							console.log('授权失败 手机号失败', err)
						}
					})



				} else {
					return uni.utils.toast(resp.message);
				}
			})
		},
		fail: err => {
			console.log('my.getAuthCode 调用失败', err)
		}
	});

}
export function handleAuthError() {
	console.log('拒绝获取手机号')
}
export function uuIds() {
	var s = [];
	var hexDigits = '0123456789abcdef';
	for (var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = '-';

	var uuid = s.join('');
	return uuid;
	// console.log(uuid);
}